package shopping.web.filter;

import shopping.utils.RRHolder;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class RRFilter implements Filter {
    public void destroy() {
    }
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //创建req和resp对象
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        //将request和response放入当前线程中
        /*
        ThreadLocal<HttpServletRequest> requestThreadLocal = new ThreadLocal<>();
        requestThreadLocal.set(request);
        ThreadLocal<HttpServletResponse> responseThreadLocal = new ThreadLocal<>();
        responseThreadLocal.set(response);
         */
        RRHolder.setRequest(request);
        RRHolder.setResponse(response);

        chain.doFilter(req, resp);
    }
    public void init(FilterConfig config) throws ServletException {

    }
}
